package com.wavehi.producer;

import com.rabbitmq.client.Channel;
import com.wavehi.util.RabbitMQFactoryUtils;

import java.io.IOException;
import java.util.UUID;

/**
 * @Description 生成者 单个确认发布
 * @Author WaveHi
 * @Date 26/12/2021 下午 8:04
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */
public class Producer_01 {
    //队列名
    private static final String QUEUE_NAME = "publishConfirm";
    //消息个数
    private static final int MESSAGE_COUNT = 1000;
    public static void main(String[] args) throws Exception {
        //1.获取连接信道
        Channel channel = RabbitMQFactoryUtils.getChannel();
        //2.声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //3.开启发布确认
        channel.confirmSelect();
        long begin = System.currentTimeMillis();
        //4.准备消息
        //5.发送消息
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            String message = UUID.randomUUID().toString();
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            boolean success = channel.waitForConfirms();
            if(success) {
                System.out.println("Message '" + message + "'" + "send completed!");
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("花费时间：" + (end - begin));//1389
    }
}
